package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Goods;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.GoodsService;

public class GoodsServiceImpl extends BaseService implements GoodsService {

    public GoodsServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public GoodsService listAllGoods(final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_goods order by updated_on desc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public GoodsService listGoodsByPage(final int page, final int limit, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_goods order by updated_on desc limit ?, ?")
            .put(PARAMS, new JsonArray().add(this.calcPage(page, limit)).add(limit));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public GoodsService getGoodsById(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_goods where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public GoodsService insertGoods(final Goods goods, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_goods (id, title, poster, short_desc, content, image_list, price, count, code, on_sell, created_on, updated_on)" +
                " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())")
            .put(PARAMS, new JsonArray()
                .add(goods.getId())
                .add(goods.getTitle())
                .add(goods.getPoster())
                .add(goods.getShortDesc())
                .add(goods.getContent())
                .add(goods.getImageList())
                .add(goods.getPrice())
                .add(goods.getCount())
                .add(goods.getCode())
                .add(goods.isOnSell()));
        System.out.println(goods.toJson());
        this.insertSql(sqlMes, this.resultUpdate(goods.getId(), tMessage, "error happened"));
        return this;
    }

    @Override
    public GoodsService deleteGoods(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "delete from ds_goods where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "error happened"));
        return this;
    }

    @Override
    public GoodsService updateGoods(final Goods goods, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_goods set title = ?, poster = ?, short_desc = ?, content = ?, image_list = ?, price = ?, count = ?, code = ?, updated_on = UNIX_TIMESTAMP() where id = ?")
            .put(PARAMS, new JsonArray()
                .add(goods.getTitle())
                .add(goods.getPoster())
                .add(goods.getShortDesc())
                .add(goods.getContent())
                .add(goods.getImageList())
                .add(goods.getPrice())
                .add(goods.getCount())
                .add(goods.getCode())
                .add(goods.getId()));
        this.updateSql(sqlMes, this.resultUpdate(goods.getId(), tMessage, "error happened"));
        return this;
    }

    @Override
    public GoodsService publish(final String id, final String onSell, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_goods set on_sell = ? where id = ?")
            .put(PARAMS, new JsonArray().add(onSell).add(id));
        this.updateSql(sqlMes, this.resultUpdate(id, tMessage, "error"));
        return this;
    }
}
